package com.eascs.admin.o2o.compensatory.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.badloan.controller.O2oBadloanRestController;
import com.eascs.admin.o2o.compensatory.entity.CompensatoryDataEnter;
import com.eascs.admin.o2o.compensatory.service.GeneratedContainerNumberService;
import com.eascs.admin.o2o.compensatory.service.O2oDCLoanService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.workflow.common.service.sequence.SequenceService;

@RestController
@RequestMapping("/rdcloan")
public class O2oDCloanRestController extends BaseRestController{
	private static Logger log = LoggerFactory.getLogger(O2oBadloanRestController.class);
	
	@Autowired
	O2oDCLoanService service;
	
	@Reference
	private SequenceService sequenceService;
	
	@Autowired
	GeneratedContainerNumberService numberService;
	
	//跳转录入界面
	@RequestMapping(value = "/toEnterCabinetData")
	public HtmlRestResult toEnterCabinetData(String loanDataId){
		ViewContext.put("loanDataId", loanDataId);
		return HtmlRestResult.popHtmlFragment("/dcloan/enter_data");
	}
	
	//代偿资料录入
	@RequestMapping(value = "/saveEnterCabinetData")
	public BaseRestResult<String> saveEnterCabinetData(CompensatoryDataEnter vo){
		BaseRestResult<String> result = null;
		Calendar cal = Calendar.getInstance();
		String today = new SimpleDateFormat("yyyyMM").format(cal.getTime()); 
		if (vo.getEnsBillNum()==null || vo.getReceiptNum()==null || vo.getRefundDetailNum()==null
				|| vo.getTransferInvitationNum()==null) {
			result = BaseRestResult.warn("数据不能为空，请确认信息是否完整");
		} else {
			String billCode = "";
			String intoCabinetCode = "";
			String cabinetNo = "";
			String createCode = "";
			// 根据id查询贷款编号
			billCode = service.findBillCodeById(vo.getLoanDataId());
			// 根据id查询省份
			String province = service.findProvinceById(vo.getLoanDataId());
			// 根据省份查询省份简称
			String provinceJC = service.findJCByProvince(province);
			// 获取编号
			createCode = sequenceService.getSeq("DC", 5);
			if (StringUtil.isBlank(createCode)) {
				result = BaseRestResult.warn("未获取到入柜编号，请重新保存");
				
			} else {
				String splitCode[] = createCode.split("-");
				intoCabinetCode = "DC-" + provinceJC + "-" + today + "-" + splitCode[1];
				//生成柜号
				cabinetNo = numberService.getContainerNumber();
				CompensatoryDataEnter entity = new CompensatoryDataEnter();
				entity.setBillCode(billCode);
				entity.setLoanDataId(vo.getLoanDataId());
				entity.setEnterType("1");
				entity.setIntoCabinetCode(intoCabinetCode);
				entity.setEnsBillNum(vo.getEnsBillNum());
				entity.setEnsBillResidueNum(vo.getEnsBillNum());
				entity.setEnsBillLoanNum(0);
				entity.setReceiptNum(vo.getReceiptNum());
				entity.setReceiptResidueNum(vo.getReceiptNum());
				entity.setReceiptLoanNum(0);
				entity.setRefundDetailNum(vo.getRefundDetailNum());
				entity.setRefundDetailResidueNum(vo.getRefundDetailNum());
				entity.setRefundDetailLoanNum(0);
				entity.setTransferInvitationNum(vo.getTransferInvitationNum());
				entity.setTransferInvitationResidueNum(vo.getTransferInvitationNum());
				entity.setTransferInvitationLoanNum(0);
				entity.setCabinetNo(cabinetNo);
				try {
					service.saveEnterInfo(entity);
					result = BaseRestResult.info("贷款资料录入成功").andCloseActionDialog().andRefresh();
				} catch (Exception e) {
					log.info("贷款资料录入异常:贷款编号为:[{}]", billCode);
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	//跳转修改界面
	@RequestMapping(value="/toUpdateCabinetData")
	public HtmlRestResult toUpdateCabinetData(String loanDataId){
		CompensatoryDataEnter entity =  service.findloanDataIdById(loanDataId);
		ViewContext.put("vo", entity);
		ViewContext.put("loanDataId", loanDataId);
		return HtmlRestResult.popHtmlFragment("/dcloan/update_data");
		
	}
	
	// 代偿资料修改
	@RequestMapping(value = "/updateCabinetData")
	public BaseRestResult<String> updateCabinetData(CompensatoryDataEnter vo) {
		BaseRestResult<String> result = null;
		// 根据id查询数据库中的数据
		CompensatoryDataEnter data = service.findloanDataIdById(vo.getLoanDataId());
		if (vo.getEnsBillNum() == null || vo.getReceiptNum() == null || vo.getRefundDetailNum() == null
				|| vo.getTransferInvitationNum() == null) {
			result = BaseRestResult.warn("数据不能为空，请确认信息是否完整");
		} else {
			if (vo.getTransferInvitationNum() < data.getTransferInvitationNum()) {
				result = BaseRestResult.warn("银行债权转让函数量不得小于原数量");
			} else if (vo.getReceiptNum() < data.getReceiptNum()) {
				result = BaseRestResult.warn("EA代偿证明或国内支付业务收款回单数量不得小于原数量");
			} else if (vo.getEnsBillNum() < data.getEnsBillNum()) {
				result = BaseRestResult.warn("催收及资产转让通知函EMS快递单数量不得小于原数量");
			} else if (vo.getRefundDetailNum() < data.getRefundDetailNum()) {
				result = BaseRestResult.warn("客户还款记录明细数量不得小于原数量");
			} else {
				CompensatoryDataEnter entity = new CompensatoryDataEnter();
				entity.setId(data.getId());
				entity.setTransferInvitationNum(vo.getTransferInvitationNum());
				entity.setReceiptNum(vo.getReceiptNum());
				entity.setRefundDetailNum(vo.getRefundDetailNum());
				entity.setEnsBillNum(vo.getEnsBillNum());
				try {
					service.updateEnterInfo(entity);
					result = BaseRestResult.info("贷款资料修改成功").andCloseActionDialog().andRefresh();
				} catch (Exception e) {
					log.info("贷款资料修改异常:贷款编号为:[{}]", data.getBillCode());
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	//跳转查看界面
	@RequestMapping(value = "/toLookCabinetData")
	public HtmlRestResult toLookCabinetData(String loanDataId) {
		CompensatoryDataEnter entity = service.findloanDataIdById(loanDataId);
		ViewContext.put("vo", entity);
		ViewContext.put("loanDataId", loanDataId);
		return HtmlRestResult.popHtmlFragment("/dcloan/look_data");

	}
}
